// pages/preview/preview.js
const App = getApp();
const navigationBarHeight = (App.statusBarHeight + 44) + 'px'
var config = require('../../utils/config');
var net = require('../../utils/net.js');
var util = require('../../utils/util.js');

Page({

  /**
   * 页面的初始数据
   */
  data: {
    navigationBarTitle: 'Japitta',
    navigationBarHeight,

    navbar: ['全部', '予約確認中', '予約完了', '取消', '来店済み','過去予約'],
    currentTab: 0 ,
    dataArr: [],
    hasMore: true,
    hasRefesh: false,
    scrollTop: 0,//刷新用的
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    new App.ToastPannel();

    this.page = 1;  
  },

  onShow:function(){

    this.refreshData();
    
  },
  
  requestData:function(params){
    this.setData({
      hasRefresh: true
    })
    var url = config.service.orderListUrl;
    net.doRequest(url, params, 'GET', true, net.Token, res => {
      // 等待框去掉
      this.setData({
        hasRefesh: false
      })

      if (res.code == 0) {
        this.totalPage = res.data.totalPage
        var arr = res.data.items;
        if (arr.length < 5) {
          this.setData({
            hasMore: false,
          })
        }
        if(arr.length > 0){
          for(var i=0;i<arr.length;i++){
            var item = arr[i];
            item.shopImg1 = config.service.picUrl + item.shopImg1
            item.orderTime = util.zhuanHuaTime(item.orderTime,'Y-M-D h:m')
          }
        }
        if(this.page == 1){
          this.setData({
            dataArr: arr,
            scrollTop: 0,
          })
        }else{
          this.setData({
            dataArr: this.data.dataArr.concat(arr)
          })
        }
        
      } else {
        this.show(res.message);
      }

    })

  },

  
  /**
   * navBar点击
   */
  navbarTap: function (e) {
    this.setData({
      currentTab: e.currentTarget.dataset.idx
    })
    this.refreshData(); 
  },

  refreshData:function(){
    this.setData({
      hasMore:true
    })
    var state = null
    if (this.data.currentTab == 1) {
      state = 0
    }
    if (this.data.currentTab == 2) {
      state = 1
    }
    if (this.data.currentTab == 3) {
      state = 3
    }
    if (this.data.currentTab == 4) {
      state = 2
    }
    if (this.data.currentTab == 5){
      state = 5
    }

    this.page = 1;
    this.totalPage = 1;
    var params = {}
    if (state != null) {
      params = {
        page: this.page,
        size: 5,
        state: state
      }
    } else {
      params = {
        page: this.page,
        size: 5,
      }
    }
    this.requestData(params); 
  },

  requestMore:function(){
    console.log('记载');
    console.log(this.data.hasMore, this.data.hasRefesh)
    if (!this.data.hasMore || this.data.hasRefesh) {
      return;
    }
    console.log('记载2');
    if(this.page==this.totalPage){
      this.setData({
        hasMore:false
      })
      this.show('加载完毕');
      return;
    }

    this.setData({
      hasRefesh: true,
    })

    var state = null
    if (this.data.currentTab == 1) {
      state = 0
    }
    if (this.data.currentTab == 2) {
      state = 1
    }
    if (this.data.currentTab == 3) {
      state = 3
    }
    if (this.data.currentTab == 4) {
      state = 2
    }
    if (this.data.currentTab == 5){
      state = 5
    }

    this.page ++;
    var params = {}
    if (state != null) {
      params = {
        page: this.page,
        size: 5,
        state: state
      }
    } else {
      params = {
        page: this.page,
        size: 5,
      }
    }
    this.requestData(params); 

  },

  itemClick: function (e) {
    var index = e.currentTarget.dataset.index;
    console.log('当前点击', index);
    var model = this.data.dataArr[index];
    wx.navigateTo({
      url: './preDetail?orderId=' + model.orderId,
    });
  },
  

})